package com.ruoyi.source.utils;

import com.ruoyi.source.config.MinioProperties;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;
public class MyMinioUtils {

    private MinioClient minioClient;

    private MinioProperties minioProperties;


    public MyMinioUtils(MinioClient minioClient, MinioProperties minioProperties) {
        this.minioClient = minioClient;
        this.minioProperties = minioProperties;
    }

    public String uploadFile(MultipartFile file) throws Exception {
        String filename = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        InputStream inputStream = file.getInputStream();
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(minioProperties.getBucketName())
                .object(filename)
                .stream(inputStream, file.getSize(), -1)
                .contentType(file.getContentType())
                .build());
        inputStream.close();
        return minioProperties.getEndpoint() + "/" + minioProperties.getBucketName() + "/" + filename;
    }
}
